#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

float read_next() {
    float *tmp;
    float return_val;

    #pragma omp single copyprivate(tmp)
    {
        tmp = (float *)malloc(sizeof(float));
    } /* 只需等待 */

    #pragma omp masked
    {
        int thread_id = omp_get_thread_num(); // 获取当前线程 ID
        printf("线程 %d: 输入一个浮点数: \n", thread_id);
        scanf("%f", tmp);
    }

    #pragma omp barrier
    return_val = *tmp;

    #pragma omp single nowait
    {
        free(tmp);
    }

    return return_val;
}

int main() {
    float result;
    int num_threads = 4; // 使用的线程数量

    #pragma omp parallel num_threads(num_threads)
    {
        int thread_id = omp_get_thread_num();
        result = read_next();

        // 使用中文打印结果
        printf("线程 %d: 读取的值 = %f\n", thread_id, result);
    }

    return 0;
}